package com.qfedu.controller;

import com.qfedu.entity.ResponseData;
import com.qfedu.entity.User;
import com.qfedu.entity.Week;
import com.qfedu.service.WeekSerivice;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;


@RestController
public class WeekController {
    @Autowired
    private WeekSerivice weekSerivice;

    //查询所有周报
    @RequestMapping("/findAllWeek")

    public ResponseData findAllWeek(int page,int limit){
    return weekSerivice.findAllWeek(page,limit);
    }
    //添加周报
    @RequestMapping("/addWeek")
    public ResponseData addWeek(Week week, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        ResponseData responseData = new ResponseData();
        week.setuNo(user.getNo());
        week.setCreateTime(new Date(System.currentTimeMillis()));
        Boolean result = weekSerivice.addWeek(week);
        if (result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    //删除周报
    @RequestMapping("/deleteWeek")
    public ResponseData deleteWeek(int id){
        Boolean result = weekSerivice.deleteWeek(id);
        ResponseData responseData = new ResponseData();
        if (result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;
    }
    //根据id查周报
    @RequestMapping("/findWeekById")
    public ResponseData findWeekById(int id){
        ResponseData responseData = new ResponseData();
        Week week = weekSerivice.findWeekById(id);
        responseData.setCode(1);
        responseData.setData(week);
        return responseData;
    }
    //更新周报
    @RequestMapping("/updateWeek")
    public ResponseData updateWeek(Week week){
        Boolean result = weekSerivice.updateWeek(week);
        ResponseData responseData = new ResponseData();
        if (result){
            responseData.setCode(ResponseData.SUCCESS_CODE);// 成功
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);// 成功
            responseData.setMsg(ResponseData.FAIL_MSG);
        }
        return responseData;

    }


}
